#include "commonpage.h"
#include "ui_commonpage.h"
#include "musiclistitem.h"
#include <QDebug>

CommonPage::CommonPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    //关闭水平滚动条
    ui->PageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //设置ListLabelBox中QLabel字体
    QFont font;
    font.setFamily("Microsoft YaHei"); // 字体名（可换成“微软雅黑”、“SimSun”等）
    font.setPixelSize(14);             // 字体大小（像素）
    font.setBold(false);                // 是否加粗
    ui->musicNameLabel->setFont(font);
    ui->musicSingerLabel->setFont(font);
    ui->musicAlbumLabel->setFont(font);
    ui->musicSongTimeLabel->setFont(font);
    ui->PageTittle->setFont(font);

    //设置播放全部按钮字体格式
    QFont btnFont;
    btnFont.setFamily("Microsoft YaHei"); // 字体名（可换成“微软雅黑”、“SimSun”等）
    btnFont.setPixelSize(15);             // 字体大小（像素）
    btnFont.setBold(false);                // 是否加粗
    ui->playAllBtn->setFont(btnFont);
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUi(const QString &text, const QString &imagePath)
{
    //设置文本
    ui->PageTittle->setText(text);

    // 设置到 QLabel 上显示
    ui->musicImageLabel->setPixmap(QPixmap (imagePath));
    ui->musicImageLabel->setScaledContents(true);
}

void CommonPage::setCommonPageType(const PageType pagetype)
{
    this->pageType = pagetype;
}

void CommonPage::addMusicToCommonPage(MusicList &musicList)
{
    musics.clear();
    for(auto& music : musicList){
        switch(this->pageType)
        {
            case LIKE_PAGE:
                if(music->getIsLike() == true){
                    musics.push_back(music);
                }
                break;
            case LOCAL_PAGE:
                musics.push_back(music);
                break;
            case RECENT_PAGE:
                if(music->getIsHistory() == true){
                    musics.push_back(music);
                }
                break;
        }
    }
}

void CommonPage::reFresh(MusicList& musicList)
{
    addMusicToCommonPage(musicList);
    //清除原本PageMusicList的所有item项
    ui->PageMusicList->clear();

    for(auto music : musics){
        // 不要给自定义item设置 this 为父对象，Qt自己会管理
        MusicListItem *item = new MusicListItem();
        item->setMusicName(music->getName());
        item->setMusicSinger(music->getSinger());
        item->setMusicAlbum(music->getAlbum());
        item->setMusicSongTime(music->getSongTime());
        item->setLikeMusic(music->getIsLike());

        QListWidgetItem *widgetItem = new QListWidgetItem(ui->PageMusicList);
        // 用 viewport 宽度来设置 sizeHint
        int w = ui->PageMusicList->viewport()->width();
        widgetItem->setSizeHint(QSize(w, item->height()));

        ui->PageMusicList->setItemWidget(widgetItem, item);

        connect(item,&MusicListItem::isLikeChanged,this,[=](bool like){
            music->setIsLike(like);
            emit updateLikeMusic();
        });
    }
    //触发窗口重绘paintEvent
    this->repaint();
}

void CommonPage::addMusicToPlaylist(QMediaPlaylist &playlist)
{
    playlist.clear();
    for(auto& music:musics){
        playlist.addMedia(music->getUrl());
    }
}

Music* CommonPage::getMusic(int idx)
{
    if(idx >= musics.size()){
        return nullptr;
    }
    return musics[idx];
}

void CommonPage::on_playAllBtn_clicked()
{
    emit playAllSignal(this);
}


void CommonPage::on_PageMusicList_doubleClicked(const QModelIndex &index)
{
    //发送信号, 通知qqMusic上层播放歌曲
    emit musicListDoubleClicked(this,index.row());
}

